2 * Copyright (C) 2023 by Claudio Cambra <claudio.cambra@nextcloud.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 import NCDesktopClientSocketKit
19 import NextcloudFileProviderKit
22 let AuthenticationTimeouts: [UInt64] = [ // Have progressively longer timeouts to not hammer server
23 3_000_000_000, 6_000_000_000, 30_000_000_000, 60_000_000_000, 120_000_000_000, 300_000_000_000
26 extension FileProviderExtension: NSFileProviderServicing, ChangeNotificationInterface {
28 This FileProviderExtension extension contains everything needed to communicate with the client.
29 We have two systems for communicating between the extensions and the client.
31 Apple's XPC based File Provider APIs let us easily communicate client -> extension.
32 This is what ClientCommunicationService is for.
34 We also use sockets, because the File Provider XPC system does not let us easily talk from
36 We need this because the extension needs to be able to request account details. We can't
37 reliably do this via XPC because the extensions get torn down by the system, out of the control
38 of the app, and we can receive nil/no services from NSFileProviderManager. Once this is done
41 func supportedServiceSources(
42 for itemIdentifier: NSFileProviderItemIdentifier,
43 completionHandler: @escaping ([NSFileProviderServiceSource]?, Error?) -> Void
45 Logger.desktopClientConnection.debug("Serving supported service sources")
46 let clientCommService = ClientCommunicationService(fpExtension: self)
47 let fpuiExtService = FPUIExtensionServiceSource(fpExtension: self)
48 let services: [NSFileProviderServiceSource] = [clientCommService, fpuiExtService]
49 completionHandler(services, nil)
50 let progress = Progress()
51 progress.cancellationHandler = {
52 let error = NSError(domain: NSCocoaErrorDomain, code: NSUserCancelledError)
53 completionHandler(nil, error)
58 @objc func sendFileProviderDomainIdentifier() {
59 let command = "FILE_PROVIDER_DOMAIN_IDENTIFIER_REQUEST_REPLY"
60 let argument = domain.identifier.rawValue
61 let message = command + ":" + argument + "\n"
62 socketClient?.sendMessage(message)
65 private func signalEnumeratorAfterAccountSetup() {
66 guard let fpManager = NSFileProviderManager(for: domain) else {
67 Logger.fileProviderExtension.error(
68 "Could not get file provider manager for domain \(self.domain.displayName, privacy: .public), cannot notify after account setup"
73 assert(ncAccount != nil)
75 fpManager.signalErrorResolved(NSFileProviderError(.notAuthenticated)) { error in
77 Logger.fileProviderExtension.error(
78 "Error resolving not authenticated, received error: \(error!.localizedDescription)"
83 Logger.fileProviderExtension.debug(
84 "Signalling enumerators for user \(self.ncAccount!.username) at server \(self.ncAccount!.serverUrl, privacy: .public)"
91 guard let fpManager = NSFileProviderManager(for: domain) else {
92 Logger.fileProviderExtension.error(
93 "Could not get file provider manager for domain \(self.domain.displayName, privacy: .public), cannot notify changes"
98 fpManager.signalEnumerator(for: .workingSet) { error in
100 Logger.fileProviderExtension.error(
101 "Error signalling enumerator for working set, received error: \(error!.localizedDescription, privacy: .public)"
107 @objc func setupDomainAccount(
108 user: String, userId: String, serverUrl: String, password: String
110 let account = Account(user: user, id: userId, serverUrl: serverUrl, password: password)
111 guard account != ncAccount else { return }
115 account: account.ncKitAccount,
120 userAgent: "Nextcloud-macOS/FileProviderExt",
121 nextcloudVersion: 25,
124 var authAttemptState = AuthenticationAttemptResultState.connectionError // default
126 // Retry a few times if we have a connection issue
127 for authTimeout in AuthenticationTimeouts {
128 authAttemptState = await ncKit.tryAuthenticationAttempt(account: account)
129 guard authAttemptState == .connectionError else { break }
131 Logger.fileProviderExtension.info(
132 "\(user, privacy: .public) authentication try timed out. Trying again soon."
134 try? await Task.sleep(nanoseconds: authTimeout)
137 switch (authAttemptState) {
138 case .authenticationError:
139 Logger.fileProviderExtension.info(
140 "\(user, privacy: .public) authentication failed due to bad creds, stopping"
143 case .connectionError:
144 // Despite multiple connection attempts we are still getting connection issues.
145 // Connection error should be provided
146 Logger.fileProviderExtension.info(
147 "\(user, privacy: .public) authentication try failed, no connection."
151 Logger.fileProviderExtension.info(
153 Authenticated! Nextcloud account set up in File Provider extension.
154 User: \(user, privacy: .public) at server: \(serverUrl, privacy: .public)
161 changeObserver = RemoteChangeObserver(
163 remoteInterface: ncKit,
164 changeNotificationInterface: self,
167 ncKit.setup(delegate: changeObserver)
168 signalEnumeratorAfterAccountSetup()
173 @objc func removeAccountConfig() {
174 Logger.fileProviderExtension.info(
175 "Received instruction to remove account data for user \(self.ncAccount!.username, privacy: .public) at server \(self.ncAccount!.serverUrl, privacy: .public)"
180 func updatedSyncStateReporting(oldActions: Set<UUID>) {
183 guard oldActions.isEmpty != syncActions.isEmpty else {
188 let command = "FILE_PROVIDER_DOMAIN_SYNC_STATE_CHANGE"
189 var argument: String?
190 if oldActions.isEmpty, !syncActions.isEmpty {
191 argument = "SYNC_STARTED"
192 } else if !oldActions.isEmpty, syncActions.isEmpty {
193 argument = errorActions.isEmpty ? "SYNC_FINISHED" : "SYNC_FAILED"
199 guard let argument else { return }
200 Logger.fileProviderExtension.debug("Reporting sync \(argument)")
201 let message = command + ":" + argument + "\n"
202 socketClient?.sendMessage(message)